/* ProductBusiness.java
 *
 * Project/Product: GW
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   Administrator	2008-7-9	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package system.download;

import java.util.Map;
import java.util.Vector;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import framework.core.DBConnection;

public class ProductBusiness {
    private Business business = null;
    
    ProductBusiness(){
        business = Business.getBusiness();
    }
    
    //最终所需要的结果列
    private String[] colNamesPro = new String[]{
            "CONCENTRICITY_CODE",
            "CATEGORY_CODE",
            "NAME",
            "SPEC",
            "DRAWING_PIECE_CODE",
            "PRODUCT_BRAND",
            "PROVIDER_CODE",
            "MANUFATURER",
            "AREA",
            "MODEL",
            "QUALITY_ASSURE_PERIOD",
            "ADAPT_MACHINE_TYPE"};    
    
    /** 查询出所需要的产品   */
    public void searchProduct(String preDay, DBConnection con) {
        String sql = "select * from t_product where last_update_time >= '"+preDay+business.start+"' and last_update_time <= '"+preDay+business.end+"'";
        Vector vec = business.setMap(colNamesPro, sql, con);
        Map map = null;
        //获得路径
        String path = FileBusiness.getRealPath();
        String filename = business.getPreDayNum()+"_000000_材料编码文件.xls";
        if(vec.size() > 0){
            //创建文件
            HSSFWorkbook book = createProduct(preDay,filename, vec);
            //写文件
            boolean result = FileBusiness.createExcelFile(path, book, filename);
            //写报告
            if(result) 
                map = business.createMap("000000", "T_PRODUCT", path, filename, "成功");
            else 
                map = business.createMap("000000", "T_PRODUCT", path, filename, "失败");
        }else 
            map = business.createMap("000000", "T_PRODUCT", path, filename, "无更新");
        business.writeReport(con,map);
    }
    
    
    public HSSFWorkbook createProduct(String preDay,String sheetName,Vector vec) {
        //创建文件对象
        HSSFWorkbook tWb = new HSSFWorkbook();
        //创建sheet
        HSSFSheet tSheet = tWb.createSheet();
        //命名sheet
        tWb.setSheetName(0,sheetName,HSSFCell.ENCODING_UTF_16);
        //创建行
        HSSFRow tRow = tSheet.createRow((short)0);
        int size = vec.size();
        HSSFCell tCell0  = business.createCell(tWb,tRow,(short)0, "总行数:");
        HSSFCell tCell1  = business.createCell(tWb,tRow,(short)1, String.valueOf(size));
        for(int i=0;i<size;i++){
            Map map = (Map)vec.get(i);
            HSSFRow row = tSheet.createRow((short)i+1);
            HSSFCell cell0  = business.createCell(tWb,row,(short)0, map.get("CONCENTRICITY_CODE")== null ? "" : String.valueOf(map.get("CONCENTRICITY_CODE")));
            HSSFCell cell1  = business.createCell(tWb,row,(short)1, map.get("CATEGORY_CODE")== null ? "" : String.valueOf(map.get("CATEGORY_CODE")));
            HSSFCell cell2  = business.createCell(tWb,row,(short)2, map.get("NAME")== null ? "" : String.valueOf(map.get("NAME")));
            HSSFCell cell3  = business.createCell(tWb,row,(short)3, map.get("SPEC")==null ? "" : String.valueOf(map.get("SPEC")));
            HSSFCell cell4  = business.createCell(tWb,row,(short)4, map.get("DRAWING_PIECE_CODE")== null ? "" : String.valueOf(map.get("DRAWING_PIECE_CODE")));
            HSSFCell cell5  = business.createCell(tWb,row,(short)5, map.get("PRODUCT_BRAND")== null ? "" : String.valueOf(map.get("SPEC")));
            HSSFCell cell6  = business.createCell(tWb,row,(short)6, map.get("PROVIDER_CODE")== null ? "" : String.valueOf(map.get("PROVIDER_CODE")));
            HSSFCell cell7  = business.createCell(tWb,row,(short)7, map.get("MANUFATURER")== null ? "" : String.valueOf(map.get("MANUFATURER")));
            HSSFCell cell8  = business.createCell(tWb,row,(short)8, map.get("AREA")== null ? "" : String.valueOf(map.get("AREA")));
            HSSFCell cell9  = business.createCell(tWb,row,(short)9, map.get("MODEL")== null ? "" : String.valueOf(map.get("MODEL")));
            HSSFCell cell10 = business.createCell(tWb,row,(short)10,map.get("QUALITY_ASSURE_PERIOD")== null ? "" : String.valueOf(map.get("QUALITY_ASSURE_PERIOD")));
            HSSFCell cell11 = business.createCell(tWb,row,(short)11,map.get("ADAPT_MACHINE_TYPE") == null ? "" : String.valueOf(map.get("ADAPT_MACHINE_TYPE")));
        } 
        return tWb;
    }
}
